package com.ee.chat.api.controller;

import com.ee.chat.api.service.IIMService;
import com.ee.chat.common.bean.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author Shane
 * @Date 2019/10/5 17:48
 */
@RestController
@RequestMapping("im")
public class IMController {
    private Logger log = LoggerFactory.getLogger(IMController.class);

    @Resource
    IIMService imService;

    /**
     * 礼物列表
     * @Author Shane
     * @Date 2019/10/2916:14
     * @param
     * @return
     **/
    @RequestMapping("getGifts")
    public Result getGifts(@RequestParam(required = false) String token){
        try {
            return imService.getGifts(token);
        } catch(Exception e){
            log.info("【获取礼物列表错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 预开始通话
     * @Author Shane
     * @Date 2019/12/24 18:03
     * @param
     * @return
     **/
    @RequestMapping("beforeStartCall")
    public Result beforeStartCall(@RequestParam String token, @RequestParam Integer toUserId){
        try {
            return imService.beforeStartCall(token, toUserId);
        } catch(Exception e){
            log.info("【预开始通话错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 开始通话
     * @Author Shane
     * @Date 2019/10/2916:24
     * @param toUserId  被呼叫者id
     * @param channelId 频道id
     * @return
     **/
    @RequestMapping("startCall")
    public Result startCall(@RequestParam String token, @RequestParam Integer toUserId, @RequestParam String channelId, @RequestParam(required = false) String callId){
        try {
            return imService.startCall(token, toUserId, channelId, callId);
        } catch(Exception e){
            log.info("【开始通话错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 结束通话
     * @Author Shane
     * @Date 2019/10/2916:24
     * @param channelId 频道id
     * @return
     **/
    @RequestMapping("endCall")
    public Result endCall(@RequestParam String token, @RequestParam String channelId){
        try {
            return imService.endCall(token, channelId);
        } catch(Exception e){
            log.info("【结束通话错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 获取通话评价信息
     * @Author Shane
     * @Date 2019/11/1 15:57
     * @param channelId 频道id
     * @return
     */
    @RequestMapping("getCallEvaluateInfo")
    public Result getCallEvaluateInfo(@RequestParam String token, @RequestParam String channelId){
        try {
            return imService.getCallEvaluateInfo(token, channelId);
        } catch(Exception e){
            log.info("【获取通话评价内容错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 通话评价
     * @Author Shane
     * @Date 2019/11/1 17:19
     * @param channelId 频道id
     * @param type      评价，0-好评  1-差评
     * @param lableIds  评价标签，多个id以“,”分隔
     * @return
     */
    @RequestMapping("callEvaluate")
    public Result callEvaluate(@RequestParam String token, @RequestParam String channelId, @RequestParam Integer type, @RequestParam String lableIds){
        try {
            return imService.callEvaluate(token, channelId, type, lableIds);
        } catch(Exception e){
            log.info("【通话评价错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 获取拉黑状态
     * @Author Shane
     * @Date 2019/11/6 15:06
     * @param toUserId  被查询的用户id
     * @return
     **/
    @RequestMapping("getBlock")
    public Result getBlock(@RequestParam String token, @RequestParam Integer toUserId){
        try {
            return imService.getBlock(token, toUserId);
        } catch(Exception e){
            log.info("【获取用户拉黑状态错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 拉黑/取消拉黑
     * @Author Shane
     * @Date 2019/11/6 16:04
     * @param toUserId 被操作的用户id
     * @param block 0-取消拉黑  1-拉黑
     * @return
     **/
    @RequestMapping("block")
    public Result block(@RequestParam String token, @RequestParam Integer toUserId, @RequestParam Integer block){
        try {
            return imService.block(token, toUserId, block);
        } catch(Exception e){
            log.info("【拉黑用户错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 投诉
     * @Author Shane
     * @Date 2019/11/6 16:05
     * @param toUserId  投诉的用户
     * @param content   投诉的内容
     * @param images    图片集合
     * @return
     **/
    @RequestMapping("complaint")
    public Result complaint(@RequestParam String token, @RequestParam Integer toUserId, @RequestParam String content,
                            @RequestParam(required = false) String images){
        try {
            return imService.complaint(token, toUserId, content, images);
        } catch(Exception e){
            log.info("【投诉用户错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 获取消息提醒
     * @Author Shane
     * @Date 2020/7/20 14:54
     * @param
     * @return
     */
    @RequestMapping("getMoreMsgRemind")
    public Object getMoreMsgRemind(@RequestParam String token) throws Exception{
        try {
            return imService.getMoreMsgRemind(token);
        } catch(Exception e){
            log.error("获取消息提醒错误");
            throw new Exception(e);
        }
    }

    /**
     * IM通讯回调
     * @Author Shane
     * @Date 2019/11/15 10:47
     * @param
     * @return
     **/
    @RequestMapping("imCallback")
    public Object imCallback(@RequestBody String message){
        return imService.imCallback(message);
    }

    /**
     * IM消息抄送
     * @Author Shane
     * @Date 2020/2/25 22:02
     * @param
     * @return
     */
    @RequestMapping("imCopyCallback")
    public Object imCopyCallback(@RequestBody String message) throws Exception{
        try {
            return imService.imCopyCallback(message);
        } catch(Exception e){
            log.error("IM消息抄送错误");
            throw new Exception(e);
        }
    }

    /**
     * 获取通话token
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("getCallToken")
    public Result getCallToken(@RequestParam String token, @RequestParam Long uid) throws Exception{
        try {
            return imService.getCallToken(token, uid);
        } catch(Exception e){
            log.error("获取通话token错误");
            throw new Exception(e);
        }
    }
}
